// pages/userSide/perCenter/perFeedback/perFeedback.js
Page({
  data: {
    // 字数限制
    current: 0,
    max: 300,
    // 上传图片系列
    show:true,
    hidden:false,
    imgs: [],
    content:"",
    image:[],
    upimgs:[],
    desc:"",
    shangchuan:[]
  },
  // 文本框字数限制
  limit: function (e)  {
    console.log(e)
    var value = e.detail.value;
    var length = parseInt(value.length);
    if  (length > this.data.noteMaxLen) {
      return;
    }
    this.setData({
      current: length,
      content:value
    });
  },
  // 确定
  getIndex(){
    var that=this
    wx.request({
      url: 'https://baojian.dobest.site/index.php?s=/api/Archives/feedBack&wxapp_id=10001', 
      data: {
        image:that.data.shangchuan,
        desc:that.data.content,
      },
      method:"POST",
      success (res) {
        wx.showToast({
          title: '反馈成功',
          icon: 'none',
          duration: 2000
        })
        that.setData({
          imgs: '',
          text:'',
          current:0
        })
      }
    })
  },
  // 上传图片
  chooseImg: function (e) {
        var _this = this;
        var imgs = this.data.imgs;
        var uparr = this.data.upimgs;
        wx.chooseImage({
          count: 4 - imgs.length, // 默认9
          sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
          sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
          success: function (res) {
            wx.showLoading({
              title: '正在上传',
              mask:true
            })
            let uparr = [];
            // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
            var tempFilePaths = res.tempFilePaths;
            for(let i=0;i<tempFilePaths.length;i++){
              imgs.push(tempFilePaths[i]);
              uparr.push(upimg(tempFilePaths[i]));
            }
            _this.setData({
              imgs:imgs
            })
            console.log(_this.data.shangchuan)
            Promise.all(uparr).then((res)=>{
              console.log('success');
              console.log(res);
              _this.setData({
                upimgs:uparr,
                // shangchuan:res
              })
              _this.data.shangchuan.push(res[0]);
              console.log(_this.data.shangchuan)
              wx.hideLoading()
            }).catch((err)=>{
              console.log('err')
              wx.showToast({
                title: '上传失败',
                icon:'none',
                duration:2000
              })
              _this.setData({
                upimgs:[],
                imgs:[]
              })
            })
            function upimg(upimgs){
              return new  Promise((res,rej)=>{
                wx.uploadFile({
                  url: 'https://baojian.dobest.site/index.php?s=/api/Archives/upload&wxapp_id=10001', //仅为示例，非真实的接口地址
                  filePath:upimgs,
                  name: 'image',
                  success (success){
                    res(JSON.parse(success.data).data);
                  },
                  fail(err){
                    rej()
                  }
                })
              })
            }
          }
        });
      },
  // 删除图片
  deleteImg: function (e) {
    var upimgs = this.data.upimgs
    var imgs = this.data.imgs;
    var index = e.currentTarget.dataset.index;
    imgs.splice(index, 1);
    upimgs.splice(index, 1);
    this.setData({
      imgs: imgs,
      upimgs:upimgs
    });
  },
  // 预览图片
  previewImg: function (e) {
    //获取当前图片的下标
    var index = e.currentTarget.dataset.index;
    //所有图片
    var imgs = this.data.imgs;
    wx.previewImage({
      //当前显示图片
      current: imgs[index],
      //所有图片
      urls: imgs
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})